
package com.cari.baju.yog;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cari.baju.yog.adapter.AdapterCari;
import com.cari.baju.yog.database.Database;
import com.cari.baju.yog.entity.E_Toko;
import com.cari.baju.yog.utils.Utils;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;

public class CariAllActivity extends Activity implements ConnectionCallbacks,
        OnConnectionFailedListener, OnClickListener, OnItemClickListener {

    private LocationClient locationClient;
    private Location location;
    private boolean locFound = false;

    private int category;
    private Bundle data;

    private Button btnCari;
    private EditText etCari;
    private ListView lvCari;
    private List<E_Toko> listToko;
    private AdapterCari adapter;
    private ProgressDialog pDialog;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cari_all);

        data = getIntent().getExtras();
        if (data != null) {
            category = data.getInt("cat");
        }

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading..");

        etCari = (EditText) findViewById(R.id.et_cari);
        btnCari = (Button) findViewById(R.id.btn_cari);
        lvCari = (ListView) findViewById(R.id.lv_cari);
        title = (TextView) findViewById(R.id.title_judul);

        title.setTypeface(Utils.getCustomFont(this));

        listToko = new ArrayList<E_Toko>();

        locationClient = new LocationClient(this, this, this);

        lvCari.setOnItemClickListener(this);
        btnCari.setOnClickListener(this);
    }

    private class AsyncCari extends AsyncTask<String, Void, List<E_Toko>> {
        @Override
        protected List<E_Toko> doInBackground(String... params) {
            List<E_Toko> tmp = Database.getInstance(CariAllActivity.this).searchToko(params[0],
                    Integer.parseInt(params[1]), location);
            Utils.TRACE("CariAllActivity.AsyncCari", "doInBackground : cari " + params[1]);
            return tmp;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pDialog.show();
        }

        @Override
        protected void onPostExecute(List<E_Toko> result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            if (result != null) {
                for (E_Toko e_Toko : result) {
                    listToko.add(e_Toko);
                }
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        locationClient.connect();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        locationClient.disconnect();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.cari_all, menu);
        return true;
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Connection Failed", Toast.LENGTH_SHORT).show();
        if (result.hasResolution()) {
            Toast.makeText(this, "Result Has Resolution", Toast.LENGTH_LONG).show();
            try {
                result.startResolutionForResult(this, 1);
            } catch (IntentSender.SendIntentException e) {
                Log.d("error", "" + e.getMessage());
            }
        } else {
            Toast.makeText(this, "No Resolution", Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onConnected(Bundle connectionHint) {
        // TODO Auto-generated method stub
        location = locationClient.getLastLocation();
        if (location != null)
            locFound = true;
    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if (arg0 == btnCari) {
            if (locFound) {

                if (listToko.size() > 0)
                    listToko.clear();

                adapter = new AdapterCari(this, listToko);
                lvCari.setAdapter(adapter);

                if (!TextUtils.isEmpty(etCari.getText().toString())) {

                    if (data == null)
                        category = 0;

                    new AsyncCari().execute(etCari.getText().toString(), String.valueOf(category));

                } else {
                    Toast.makeText(this, "Masukkan Kata", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "loc not found", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
        // TODO Auto-generated method stub
        Bundle data = new Bundle();
        data.putInt("id", ((E_Toko) adapter.getItem(pos)).getId());

        startActivity(new Intent(this, TokoActivity.class).putExtras(data));
    }

}
